﻿@{
    ViewBag.Title = "AddPStructForm"; ;
    Layout = "~/Views/Shared/_LayoutIndex.cshtml";
}@using LeSoft.Application.Code;
<script>
    var _organizeid = "@OperatorProvider.Provider.Current().CompanyId";
    var _pstructId = "";
    $(function () {
        InitialPage();
        GetGrid();
        GetTree();
    });
    //初始化页面
    function InitialPage() {
        //layout布局
        $('#layout').layout({
            applyDemoStyles: true,
            onresize: function () {
                $(window).resize()
            }
        });
        //resize重设(表格、树形)宽高
        $(window).resize(function (e) {
            window.setTimeout(function () {
                $('#gridTable').setGridWidth(($('.gridPanel').width()));
                $("#gridTable").setGridHeight($(window).height() - 165.5);
            }, 200);
            e.stopPropagation();
        });
    }
    function GetTree() {
        var item = {
            height: $(window).height() - 20,
            url: "../../ResourceManage/RS_PStructs/GetTreeJson?type=5&IsSubstep=true&organizeid=" + _organizeid,
            onnodeclick: function (item) {
                if (_pstructId != item.id) {
                    _pstructId = item.id;
                    $('#btn_Search').trigger("click");
                }
            }
        };
        //初始化
        $("#itemTree").treeview(item);
        if (!!_pstructId) {
            var re = new RegExp("-", "g");
            $("#itemTree_" + _pstructId.replace(re, '_')).addClass("bbit-tree-selected");
        }
    }

    //加载表格
    function GetGrid() {
        var selectedRowIndex = 0;
        var $gridTable = $('#gridTable');
        var queryJson = {
            condition: $("#queryCondition").find('.dropdown-text').attr('data-value'),
            keyword: $("#txt_Keyword").val(),
            OrganizeId: _organizeid,
            PStructId: _pstructId,
            Type: 5,
        };
        $gridTable.jqGrid({
            url: "../../ResourceManage/RS_PStructs/GetPageListJson",
            postData: { queryJson: JSON.stringify(queryJson) },
            datatype: "json", 
            height: $(window).height() - 110,
            autowidth: true,
            multiselect: true,
            colModel: [
                { label: "主键", name: "id", hidden: true },
                { label: "room主键", name: "roomid", hidden: true },
                { label: "房间名称", name: "name", index: "name", width: 300, align: "center", sortable: true },
                { label: "房间编号", name: "code", index: "code", width: 150, align: "center", sortable: true },
                { label: "房间面积", name: "area", index: "area", width: 250, align: "center", sortable: true },
                 {
                     label: "状态", name: "state", width: 100, align: "center", sortable: true,
                     formatter: function (cellvalue, options, rowObject) {
                         switch (cellvalue) {
                             case 0: return "准备房";
                             case 1: return "待租";
                             case 2: return "已选取";
                             case 3: return "已租";
                             case 4: return "自用";
                             case 5: return "已售";
                             case 6: return "续租中";
                             case 7: return "维修中";
                             default: return "";
                         }
                     }
                 },
                { label: "房间描述", name: "memo", index: "memo", width: 200, align: "center", sortable: false }
            ],
            viewrecords: true,
            rowNum: 10000,
            sortable: true,
            sortname: 'code',
            sortorder: 'asc',
            rownumbers: true,
            gridview: true,
            onSelectRow: function (status) {
                selectedRowIndex = $("#" + this.id).getGridParam('selrow');

            },
            gridComplete: function () {
                var rowIds = $gridTable.jqGrid('getDataIDs');
                for (var k = 0; k < rowIds.length; k++) {
                    var curRowData = $gridTable.jqGrid('getRowData', rowIds[k]);
                    var curChk = $("#" + rowIds[k] + "").find(":checkbox");
                    curChk.attr('name', 'checkboxname');   //给每一个checkbox赋名字
                    curChk.attr('value', curRowData['MY_ID']);   //给checkbox赋值
                    curChk.attr('title', curRowData['NAME']);
                }
            },

        });
        //查询条件
        $("#queryCondition .dropdown-menu li").click(function () {
            var text = $(this).find('a').html();
            var value = $(this).find('a').attr('data-value');
            $("#queryCondition .dropdown-text").html(text).attr('data-value', value)
        });
        //查询事件
        $("#btn_Search").click(function () {
            var queryJson = {
                condition: $("#queryCondition").find('.dropdown-text').attr('data-value'),
                keyword: $("#txt_Keyword").val(),
                OrganizeId: _organizeid,
                PStructId: _pstructId,
                Type: 5,
            };
            $gridTable.jqGrid('setGridParam', {
                url: "../../ResourceManage/RS_PStructs/GetPageListJson",
                postData: { queryJson: JSON.stringify(queryJson) }
            }).trigger('reloadGrid');
        });
        //查询回车
        $('#txt_Keyword').bind('keypress', function (event) {
            if (event.keyCode == "13") {
                $('#btn_Search').trigger("click");
            }
        });
    }

    function AcceptClick(callback) {

        var Data = [];
        var $gridTable = $('#gridTable');
        var obj = $gridTable.jqGrid("getRowData");
        debugger;
        for (var i = 0; i < obj.length; i++) {
            var Ischk = $("#jqg_gridTable_" + obj[i].id).is(':checked');
            if (Ischk) {
                Data.push(obj[i]);
            }
        }
        if (Data.length <= 0) {
            top.dialogTop('至少选择一条有效数据', 'error');
            return false;
        }
        callback(Data)
        dialogMsg("操作成功！", 1);
        dialogClose();

    }

</script>
<div class="ui-layout" id="layout" style="height: 100%; width: 100%;">
    <div class="ui-layout-west">
        <div class="west-Panel" style=" overflow-y: auto; ">
            <div id="itemTree"></div>
        </div>
    </div>
    <div class="ui-layout-center">
        <div class="center-Panel">
            <div class="titlePanel">
                <div class="title-search">
                    <table>
                        <tr>
                            <td style="padding-left: 2px;">
                                <div class="btn-group" id="queryCondition">
                                    <a class="btn btn-default dropdown-text" data-toggle="dropdown">筛选条件</a>
                                    <a class="btn btn-default dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></a>
                                    <ul class="dropdown-menu">
                                        <li><a data-value="Code">编号</a></li>
                                        <li><a data-value="Name">名称</a></li>
                                    </ul>
                                </div>
                            </td>
                            <td style="padding-left: 2px;">
                                <input id="txt_Keyword" type="text" class="form-control" placeholder="请输入要查询关键字" style="width: 200px;" />
                            </td>
                            <td style="padding-left: 5px;">
                                <a id="btn_Search" class="btn btn-primary"><i class="fa fa-search"></i>&nbsp;查询</a>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>

            <div class="gridPanel">
                <table id="gridTable"></table>
            </div>
        </div>
    </div>
</div>


